(function(){
	if(typeof window.AjaxLoad === 'function')return;

	window.AjaxLoad = (function(){
		return function(config){
			var data = [], index = 0, pageSize = config.pageSize || 10,
				pageNumSize = config.pageNumSize || 6, allPageNum = 0
				selected = {count:0};

			if(config.selected){
				$.each(config.selected, function(i, d){
					selected[d] = 1;
				})
			}
			//$.extend(selected, (config.selected || {}));

			this.selected = selected;
			this.container = config.container;

			$.ajax({
				url:config.url,
				success:function(d){
					var tmp = toJSON(d);
					$.each(tmp.data, function(i, d){
						var j = parseInt(i/pageSize);
						if(typeof data[j] === 'undefined')data[j] = [];
						data[j][i%pageSize] = d;
					});
					allPageNum = parseInt(tmp.count/pageSize);
					if(tmp.count%pageSize !== 0)allPageNum++;
					$(config.container + '_pageCount').html(allPageNum);
					loadData(0);
					loadPageNum(index);
				}
			});

			function toJSON(d){
				var tmp = {data:[], count:0};
				try{
					tmp = eval('('+d+')');
				}catch(e){}
				return tmp;
			}

			function loadData(pageNum){
				if(pageNum < 0 || pageNum >= allPageNum)return;
				if(typeof data[pageNum] === 'undefined'){
					$.ajax({
						url:config.url.replace('{0}', pageNum).replace('{1}', pageNum+1).replace('{2}', pageSize),
						success:function(d){
							var tmp = {data:[], count:0};
							try{
								tmp = eval('('+d+')');
							}catch(e){}
							data[pageNum] = tmp.data;
							loadData(pageNum);
						}
					});
					return;
				}
				var ul = [];
				$.each(data[pageNum], function(i){
					ul[i] = '<li data="'+this.id+'"'+(selected[this.id]?' class="seled"':'')+' title="'+this.id+'"><div class="img"><img src="'+this.img+'" alt="" /></div><i></i><span class="name">'+this.name+'</span></li>';
				});
				$(config.container).html('<ul class="clearfix" style="width:532px;">'+ul.join('')+'</ul>');
				$(config.container + '_curPageNum').html(pageNum + 1);
			}
			function loadPageNum(i){
				var m = i*pageNumSize,
					n = m + pageNumSize,
					ret = [];

				if(n>allPageNum)n = allPageNum;
				if(m>n)return index--;
				ret.push('<a href="#" class="cur">'+(++m)+'</a>');
				loadData(m-1);
				for(;m<n;){
					ret.push('<a href="#">'+(++m)+'</a>');
				}
				$(config.pageContainer).html(ret.join(''));
			}

			$(config.page).on('click', 'a', function(){
				var o = $(this);
				o.parent().find('a').removeClass('cur');
				o.addClass('cur');
				loadData(parseInt(o.html()) - 1);
				return false;
			}).on('click', '.prev,.next', function(){
				switch(this.className){
					case 'prev':
						if(index == 0)break;
						loadPageNum(--index);
						break;
					case 'next':
						if(index+1 === allPageNum)break;
						loadPageNum(++index);
						break;
				};
				return false;
			});


			$(config.container).on('click', 'li', (config.click || function(){})).on('mouseover', 'li', function(){
				$(this).addClass('hover');
			}).on('mouseout', 'li', function(){
				$(this).removeClass('hover');
			});
		}
	})();


})();


function slider(config){

	this.content = $(config.content);
	this.container = $(config.container);

	this.content.empty();
	this.index = 1;
	this.maxLength = config.maxLength || 8;

	var _this = this;

	this.container.on('click', '.prev,.next', function(){
		switch(this.className){
			case 'prev':
				if(_this.index <= 1)break;
				_this.index--;
				_this.hide();
				$('li', _this.content).slice((_this.index-1)*_this.maxLength, _this.index*_this.maxLength).show();
				break;
			case 'next':
				var len = _this.getPageCount();
				if(_this.index >= len)break;
				_this.hide();
				$('li', _this.content).slice((_this.index++)*_this.maxLength, _this.index*_this.maxLength).show();
				break;
		}
		_this.updatePageInfo();
		return false;
	}).on('mouseenter', 'li', function(){
		$(this).addClass('hover');
	}).on('mouseleave', 'li', function(){
		$(this).removeClass('hover');
	});

};
slider.prototype = {
	hide: function(){
		$('li', this.content).hide();
	},
	getLen:function(){
		return this.content.children().length;
	},
	getPageCount:function(){
		var len = this.getLen(),
			i   = parseInt(len/this.maxLength);

		if(len === 0)return 1;
		if(len%this.maxLength != 0)return i+1;
		return i;
	},
	updatePageInfo:function(){
		$('#slider_1_pageCount').html(this.getPageCount());
		$('#slider_1_curPageNum').html(this.index);
	},
	contentChange: function(eventType){
		switch(eventType){
			case 'add':
				var len = this.getLen(), i = len%this.maxLength;

				this.index = this.getPageCount();

				if(this.index !== 1 && i > 0){
					this.hide();
					$('li:gt('+(parseInt(len/this.maxLength)*this.maxLength - 1)+')', this.content).show();
				}

				break;
			case 'remove':
				$('li', this.content).eq(this.index*this.maxLength - 1).show();
				if(this.index > this.getPageCount()){//不是最后一页
					this.index = this.getPageCount();
					$('li', this.content).slice((this.index-1)*this.maxLength, this.index*this.maxLength).show();
				}
				break;
		}
		this.updatePageInfo();
	}
}



window.onload = function(){

	var S = new slider({
		container: '#slider_1',
		content: '#slider_1_content',
		visibleNum: 8
	});

	var A = new AjaxLoad({
		url: '/api.php?type=1&s={0}&e={1}&size={2}',
		container: '#ID2',
		page: '#ID2_page',
		pageContainer: '#ID2_pageNum',
		pageSize: 21,
		pageNumSize: 1,
		click: autoAdd
	});

	function autoAdd(){
		var o = $(this);
		if(!o.hasClass('seled')){
			A.selected[o.attr('data')] = 1;
			o.addClass('seled').clone().attr('class', '').click(autoRemove).appendTo(S.content).find('span').remove();
			S.contentChange('add');
		}
		return false;
	}

	function autoRemove(){
		$(A.container + ' li[data='+$(this).attr('data')+']').removeClass('seled');
		$(this).remove();
		A.selected[$(this).attr('data')] = 0;
		S.contentChange('remove');
		return false;
	}
}
